package net.wanho.systemSettings.controller;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSONObject;
import net.wanho.commons.exception.DAOException;
import net.wanho.commons.exception.ServiceException;
import net.wanho.commons.factory.BeanFactory;
import net.wanho.commons.servlet.BaseServlet;
import net.wanho.commons.vo.AjaxResult;
import net.wanho.systemSettings.po.Menu;
import net.wanho.systemSettings.po.Position;
import net.wanho.systemSettings.service.EmmPositionService;
import net.wanho.systemSettings.service.MenuService;
import org.apache.commons.lang3.StringUtils;

@WebServlet("/EmmPositionServlet")
public class EmmPositionServlet extends BaseServlet<EmmPositionService> {
    private MenuService menuService = BeanFactory.getBean(MenuService.class) ;
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String methodName = request.getParameter("method");
		try {
			Method method = getClass().getDeclaredMethod(methodName,
					HttpServletRequest.class, HttpServletResponse.class);
			method.setAccessible(true);
			method.invoke(this, request, response);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	//查询
	protected String getAllEmmPosition(HttpServletRequest request,HttpServletResponse response) throws ServletException {
        //1.获得请求参数【模糊查询条件】
        try {
            String positionname = request.getParameter("positionname");
            //2.创建map 封装查询条件
            Map<String,String> condMap = new HashMap<>(1) ;
            //3. 判断条件是否有效【有效】
            if (StringUtils.isNotEmpty(positionname)){
                //map中存放值   key ： SQL 列名   value  用户输入关键字
                condMap.put("POSITION_NAME",positionname) ;
            }
            List<Position> positionList = super.baseService.findByCond(condMap);
                request.setAttribute("positionList",positionList);
            //查询所有菜单
            List<Menu> parentMenuList = menuService.findByCond(new HashMap<String, String>());
            request.setAttribute("parentMenuList",parentMenuList);
            return  "/WEB-INF/view/systemSettings/position/index.jsp";
        } catch (DAOException e) {
            e.printStackTrace();
            throw new ServletException(e.getMessage());
        }
    }

	//添加
	protected AjaxResult addPosition(HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
            Position position = super.parseRequest(request, Position.class);
            super.baseService.save(position);
            return AjaxResult.success();
		} catch (ServiceException e) {
			e.printStackTrace();
            return AjaxResult.fail();
		}
	}
	
	protected AjaxResult positionNameCheck(HttpServletRequest request, HttpServletResponse response){
        try {
            String positionName = request.getParameter("positionName");
            long count = super.baseService.count(positionName);
            return AjaxResult.success(count) ;
        } catch (ServiceException e) {
            e.printStackTrace();
            return AjaxResult.fail() ;
        }
	}
	protected void deletePostionCheck(HttpServletRequest request,HttpServletResponse response) throws IOException{
		JSONObject json = new JSONObject();
		response.getWriter().write(json.toString());
	}
	
	//删除
	protected void deletePosition(HttpServletRequest request,HttpServletResponse response){
		try {
             String positionid = request.getParameter("id");
             super.baseService.remove(positionid);
			response.sendRedirect(request.getContextPath()+ "/EmmPositionServlet?method=getAllEmmPosition");
		} catch (IOException | ServiceException e) {
			e.printStackTrace();
		}
	}
	
	protected String getUpdatePostionDetailById(HttpServletRequest request,HttpServletResponse response) throws ServletException {
		try {
		    String id = request.getParameter("id");
            Position position = super.baseService.findById(id);
            request.setAttribute("position",position);
            return  "/WEB-INF/view/systemSettings/position/update.jsp";
		} catch (ServiceException e) {
            e.printStackTrace();
            throw new ServletException(e.getMessage());
        }

    }
	//修改
	protected AjaxResult updatePosition(HttpServletRequest request, HttpServletResponse response){
        try {
            Position position = super.parseRequest(request, Position.class);
            super.baseService.modify(position);
            return AjaxResult.success();
        } catch (ServiceException e) {
            e.printStackTrace();
            return AjaxResult.fail();
        }
	}
	
	protected void getLinkPostionDetailById(HttpServletRequest request,HttpServletResponse response){
		try {
			request.getRequestDispatcher("/WEB-INF/view/systemSettings/position/references.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	protected void positionLinks(HttpServletRequest request,HttpServletResponse response){
		try {
			response.sendRedirect(request.getContextPath()+ "/EmmPositionServlet?method=getAllEmmPosition");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	protected String AddpositionView(HttpServletRequest request,HttpServletResponse response)
    {
        return  "/WEB-INF/view/systemSettings/position/create.jsp";
    }
}
